<?php
require_once(APPPATH . '/models/table_model.php');

class Projects_model extends Table_model{
	function Projects_model(){
		parent::Table_model();
		$this->ci->load->model('Generic_table');
		
		$this->table = 'projects';
		$this->table_name = 'Projects';
		
		$i_options = array();
		
		$this->ci->Generic_table->get_option_array( $i_options, 'industries', 'industry_id', 'industry_name');
		
		$this->id_field = 'project_id';
		
		/*$this->image_fields = array(
								'project_slides'				=> array(
																		'table'	=> 'project_slides',
																		'new_field'	=> 'new_project_slides_upload'
																	)
							);*/
		$this->addDataProcessorField('ProcessMultipleImages', 'project_slides', array('table' => 'project_slides', 'new_field' => 'new_project_slides_upload', 'sort_field' => 'project_slides_selected_order', 'table_order_field' => 'project_slide_order'));
		$this->addDataProcessorField('ProcessSingleImage', 'project_award_image', array('table' => 'project_award_images', 'new_field' => 'new_project_award_image_upload'));
		$this->addDataProcessorField('ProcessSingleImage', 'project_featured_image', array('table' => 'project_featured_images', 'new_field' => 'new_project_featured_image_upload'));
		
		$this->addDataProcessor('ProcessRelatedItems', array(
			'onEdit'	=> 'addItemOptions',
			'onSave'	=> 'saveRelatedItems',
			'onDelete'	=> 'removeAllItems'
		));
		$this->addDataProcessorField('ProcessRelatedItems', 'related_projects', array('order_by' => 'project_title', 'display_field' => 'project_title', 'table' => 'projects_rel_projects', 'item_table' => 'projects', 'item_id_field' => 'project_id', 'id_fields' => array('project_id', 'rel_project_id'), 'exclude_same_id' => true));
		$this->addDataProcessorField('ProcessRelatedItems', 'related_publications', array('order_by' => 'publication_title', 'display_field' => 'publication_title', 'table' => 'projects_rel_publications', 'item_table' => 'publications', 'id_fields' => array('project_id', 'publication_id')));
		$this->addDataProcessorField('ProcessRelatedItems', 'related_casestudies', array('order_by' => 'casestudy_title', 'display_field' => 'casestudy_title', 'table' => 'projects_rel_casestudies', 'item_table' => 'casestudies', 'id_fields' => array('project_id', 'casestudy_id')));
		
		/* can add fields that are just html elements for headers */
		$this->fields = array(
			'project_id'					=> array(
												'name' 		=> 'Project ID',
												'view'		=> false,
												'edit'		=> array( 
																'func' 		=> 'form_hidden',
																'params'	=> null,
																'label'		=> false,
																'hidden'	=> true
															)
											),
			'project_title'					=> array(
												'name' 	=> 'Title',
												'view'	=> true,
												'sort'	=> true,
												'edit'	=> array(
															'func'		=> array('Form_wrapper', 'form_input_wrapper'),
															'params'	=> array( array('id' => 'project_title') ),
															'label'		=> true
														),
												'rules'	=> 'trim|required'
											),
			'project_title_short'			=> array(
												'name' 	=> 'Short Title',
												'view'	=> true,
												'sort'	=> true,
												'edit'	=> array(
															'func'		=> array('Form_wrapper', 'form_input_wrapper'),
															'params'	=> array( array('id' => 'project_title_short') ),
															'label'		=> true
														),
												'rules'	=> 'trim|required'
											),
			'project_industry_id' 			=> array(
												'name' 	=> 'Industry',
												'view'	=> array( 
															'func' => array($this->Generic_table, 'get_field_where'),
															'params' => array('industries', 'industry_id', 'industry_name')
														),
												'sort'	=> "SELECT industry_name FROM industries WHERE industry_id = project_industry_id",
												'edit'	=> array(
															'func'		=> array('Form_wrapper', 'form_dropdown_wrapper'),
															'params'	=> array(
																				array('id'	=> 'project_industry_id'),
																				$i_options
																		),
															'label'		=> true
														)
											),
			'related_projects' 			=> array(
												'name' 	=> 'Related Projects',
												'view'	=> false,
												'sort'	=> false,
												'edit'	=> array(
															'func'		=> array('Form_wrapper', 'form_dropdown_wrapper'),
															'params'	=> array(
																				array('id'	=> 'related_projects', 'multiple' => 'multiple')
																		),
															'label'		=> true
														)
											),
			'related_publications' 		=> array(
												'name' 	=> 'Related Publications',
												'view'	=> false,
												'sort'	=> false,
												'edit'	=> array(
															'func'		=> array('Form_wrapper', 'form_dropdown_wrapper'),
															'params'	=> array(
																				array('id'	=> 'related_publications', 'multiple' => 'multiple')
																		),
															'label'		=> true
														)
											),
			'related_casestudies' 		=> array(
												'name' 	=> 'Related Casestudies',
												'view'	=> false,
												'sort'	=> false,
												'edit'	=> array(
															'func'		=> array('Form_wrapper', 'form_dropdown_wrapper'),
															'params'	=> array(
																				array('id'	=> 'related_casestudies', 'multiple' => 'multiple')
																		),
															'label'		=> true
														)
											),
			'project_call_out'			=> array(
												'name'	=> 'Project Callout',
												'view'	=> false,
												'edit'	=> array(
															'func'		=> array('Form_wrapper', 'form_textarea_wrapper'),
															'params'	=> array( array('id' => 'project_call_out') ),
															'label'		=> true
														),
												'rules'		=> 'trim'
											),
			'project_text'					=> array(
												'name'	=> 'Project Text',
												'view'	=> false,
												'edit'	=> array(
															'func' 		=> array('Form_wrapper', 'form_textarea_wrapper'),
															'params'	=> array(
																				array(
																					'id' => 'project_industry_text',
																					'class'			=> 'rte',
																					'rte_config'	=> 'project_industry_text.config',
																					'rte_toolbar'	=> 'Basic',
																					'rows'	=> '20',
																					'cols'	=> '40'
																					)
																				),
															'label'		=> true
														),
												'rules'		=> 'required_if_exists[show_in_industry]'
											),
			'project_street'				=> array(
												'name'	=> 'Street',
												'view'	=> false,
												'edit'	=> array(
															'func'		=> array('Form_wrapper', 'form_input_wrapper'),
															'params'	=> array( array('id' => 'project_street') ),
															'label'		=> true
														),
												'rules'		=> 'trim'
											),
			'project_city'					=> array(
												'name'	=> 'City',
												'view'	=> false,
												'edit'	=> array(
															'func'		=> array('Form_wrapper', 'form_input_wrapper'),
															'params'	=> array( array('id' => 'project_city') ),
															'label'		=> true
														),
												'rules'		=> 'trim'
											),
			'project_state'					=> array(
												'name'	=> 'State',
												'view'	=> false,
												'edit'	=> array(
															'func'		=> array('Form_wrapper', 'form_input_wrapper'),
															'params'	=> array( array('id' => 'project_street') ),
															'label'		=> true
														),
												'rules'		=> 'trim'
											),
			'project_architect'				=> array(
												'name'	=> 'Architect',
												'view'	=> false,
												'edit'	=> array(
															'func'		=> array('Form_wrapper', 'form_input_wrapper'),
															'params'	=> array( array('id' => 'project_architect') ),
															'label'		=> true
														),
												'rules'		=> 'trim'
											),
			'project_contractor'			=> array(
												'name'	=> 'Contractor',
												'view'	=> false,
												'edit'	=> array(
															'func'		=> array('Form_wrapper', 'form_input_wrapper'),
															'params'	=> array( array('id' => 'project_contractor') ),
															'label'		=> true
														),
												'rules'		=> 'trim'
											),
			'project_developer'				=> array(
												'name'	=> 'Developer',
												'view'	=> false,
												'edit'	=> array(
															'func'		=> array('Form_wrapper', 'form_input_wrapper'),
															'params'	=> array( array('id' => 'project_street') ),
															'label'		=> true
														),
												'rules'		=> 'trim'
											),
			'project_slides'				=> array(
												'name'				=> 'Images',
												'view'				=> false,
												'edit'				=> array(
																		'func'	=> array('Form_wrapper', 'form_dropdown_wrapper'),
																		'params'	=> array(
																						array(
																							'id'	=> 'project_slides',
																							'class'	=> 'image_selection_list',
																							'multiple'	=> 'multiple'
																						)
																					),
																		'label'	=> true
																	)
											),
			'project_slides_upload'	=> array(
												'name'		=> 'Upload New Image',
												'view'		=> false,
												'edit'		=> array(
																'func'		=> array('Form_wrapper', 'form_upload_wrapper'),
																'view'		=> false,
																'params'	=> array(
																					array(
																						'id' => 'project_slides_upload', 
																						'class' => 'image_bucket_ajax_upload',
																						'title'	=> base_url() . 'admin/projectslideupload/doupload/project_slides_upload'
																						)
																					),
																'label'		=> true
															)
											),
			'project_award_image'			=> array(
												'name' 	=> 'Award Image',
												'view'	=> false,
												'sort'	=> false,
												'edit'	=> array(
															'func'		=> array('Form_wrapper', 'form_related_item'),
															'params'	=> array(
																				array('id' => 'project_award_image'),
																				array(
																					'tag' 		=> 'img',
																					'attr' 		=> array('src' => base_url()),
																					'edit'		=> array('href' => base_url() . 'admin/images/editajax/#{id}', 'text' => 'Edit'),
																					'delete'	=> array('href' => base_url() . 'admin/images/delete/#{id}', 'text' => 'Delete')
																				)
																		),
															'label'		=> true
														)
											),
			'project_award_image_upload'	=> array(
												'name'		=> 'Upload Award Image',
												'view'		=> false,
												'edit'		=> array(
																'func'		=> array('Form_wrapper', 'form_upload_wrapper'),
																'view'		=> false,
																'params'	=> array(
																					array(
																						'id' => 'project_award_image_upload', 
																						'class' => 'single_image_ajax_upload_new',
																						'title'	=> base_url() . 'admin/projectawardimageupload/doupload/project_award_image_upload'
																						)
																					),
																'label'		=> true
															)
											),
			'project_featured_image'			=> array(
												'name' 	=> 'Featured Image',
												'view'	=> false,
												'sort'	=> false,
												'edit'	=> array(
															'func'		=> array('Form_wrapper', 'form_related_item'),
															'params'	=> array(
																				array('id' => 'project_featured_image'),
																				array(
																					'tag' 		=> 'img',
																					'attr' 		=> array('src' => base_url()),
																					'edit'		=> array('href' => base_url() . 'admin/images/editajax/#{id}', 'text' => 'Edit'),
																					'delete'	=> array('href' => base_url() . 'admin/images/delete/#{id}', 'text' => 'Delete')
																				)
																		),
															'label'		=> true
														)
											),
			'project_featured_image_upload'	=> array(
												'name'		=> 'Upload Featured Image',
												'view'		=> false,
												'edit'		=> array(
																'func'		=> array('Form_wrapper', 'form_upload_wrapper'),
																'view'		=> false,
																'params'	=> array(
																					array(
																						'id' => 'project_featured_image_upload', 
																						'class' => 'single_image_ajax_upload_new',
																						'title'	=> base_url() . 'admin/projectfeaturedimageupload/doupload/project_featured_image_upload'
																						)
																					),
																'label'		=> true
															)
											),
			'is_featured'					=> array(
												'name'	=> 'Is Featured',
												'view'	=> false,		//need to add checkbox here for editing on list page
												'edit'	=> array(
															'func'		=> array('Form_wrapper', 'form_checkbox_wrapper'),
															'params'	=> array( 
																				array('id' => 'is_featured')
																				),
															'label'		=> true
														)
											),
			'is_live'						=> array(
												'name'	=> 'Is Live',
												'view'	=> false,		//need to add checkbox here for editing on list page
												'edit'	=> array(
															'func'		=> array('Form_wrapper', 'form_checkbox_wrapper'),
															'params'	=> array( 
																				array('id' => 'is_live')
																				),
															'label'		=> true,
															'help'		=> 'Check here to display the project on the site. Uncheck to keep the data but prevent it from showing up.'
														)
											),
			'is_archive'					=> array(
												'name'	=> 'Is Archive',
												'view'	=> false,		//need to add checkbox here for editing on list page
												'edit'	=> array(
															'func'		=> array('Form_wrapper', 'form_checkbox_wrapper'),
															'params'	=> array( 
																				array('id' => 'is_archive')
																				),
															'label'		=> true,
															'help'		=> 'Check here to archive the content and remove it from the normal edit work flow.  Archived items are not displayed on the website.'
														)
											),
		
		);
	}
	
	/*function getItem( $id, $array = false){
		$item = parent::getItem( $id, $array);
		return $item;
	}*/
}
?>